// 二维码版本，校验方式和模式选择：
// https://github.com/ricmoo/qrcode/

#include <qrcode.h>
#include <Arduino_GFX_Library.h>

Arduino_DataBus *bus = new Arduino_HWSPI(PB4 /* DC */);
Arduino_GFX *gfx = new Arduino_ST7735(
  bus, GFX_NOT_DEFINED /* RST */, 0 /* rotation */, false /* IPS */,
  128 /* width */, 128 /* height */,
  2 /* col offset 1 */, 3 /* row offset 1 */,
  2 /* col offset 2 */, 1 /* row offset 2 */);

//根据字符串生成二维码
void DrawQRCode(uint8_t x, uint8_t y, char* s)
{
  QRCode qrcode;
  int version = 1;
  uint8_t qrcodeBytes[qrcode_getBufferSize(version)];

  qrcode_initText(&qrcode, qrcodeBytes, version, ECC_LOW, s);
  
  for (uint8_t i = 0; i < qrcode.size; i++) {
    for (uint8_t j = 0; j < qrcode.size; j++) {
        if (qrcode_getModule(&qrcode, j, i)) {
          //绘制黑色格子，每个格子2x2像素
          gfx->drawRect(x + j*2, y + i*2, 2, 2, BLACK);
        } else {
          //绘制白色格子，每个格子2x2像素
          gfx->drawRect(x + j*2, y + i*2, 2, 2, WHITE);
        }
    }
  }
}

void setup() {
  SPI.setMOSI(PB5);
  SPI.setSCLK(PB3);
  SPI.setSSEL(PA15);
  gfx->begin();
  gfx->fillScreen(BLACK);
  
  //绘制二维码
  DrawQRCode(10, 10, "HQYJ");
}

void loop() {
  // put your main code here, to run repeatedly:

}
